package dialog;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import model.Layer;
import utils.Parameters;
import view.MainFrame;

public class FontChooser extends JDialog {
	private JLabel m_textLabel;
	private JTextField m_textField;
	private JLabel m_colorLabel;
	private JTextField m_colorField;
	private JLabel m_preview;
	private Font m_font;
	private JLabel m_fontLabel;
	private JComboBox m_fontField;
	private JLabel m_sizeLabel;
	private JSpinner m_sizeSpinner;
	private JCheckBox m_italicCheck;
	private JCheckBox m_boldCheck;

	private JButton m_confirmButton;
	private JButton m_cancelButton;

	public FontChooser() {
		super(MainFrame.getInstance(), "Create a Text Layer.");

		initializeFrame();
		initializeField();

		this.setVisible(true);
		this.setLocationRelativeTo(MainFrame.getInstance());
	}

	public void initializeFrame() {
		this.setSize(Parameters.TL_WIDTH, Parameters.TL_HEIGHT);
		this.getContentPane().setLayout(new GridLayout(7, 2));
	}

	public void initializeField() {
		m_textLabel = new JLabel("Text: ");
		m_textField = new JTextField("preview");

		m_colorLabel = new JLabel("Color: ");
		m_colorField = new JTextField();
		m_colorField.setEditable(false);
		m_colorField.setBackground(Color.BLACK);

		m_fontLabel = new JLabel("Font: ");
		m_fontField = new JComboBox(getFontName());

		m_sizeLabel = new JLabel("Size: ");
		m_sizeSpinner = new JSpinner(new SpinnerNumberModel(14, 1, 200, 2));

		m_italicCheck = new JCheckBox("Italic");
		m_boldCheck = new JCheckBox("Bold");

		m_confirmButton = new JButton("Create");
		m_cancelButton = new JButton("Cancel");

		m_preview = new JLabel(m_textField.getText());

		m_colorField.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				Color colour = JColorChooser.showDialog(null,
						"Choose foreground colour", Color.white);
				m_preview.setForeground(colour);
				m_colorField.setBackground(colour);
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});

		m_cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		m_confirmButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainFrame
						.getInstance()
						.getDrawingPane()
						.addLayer(
								new Layer(m_font, m_preview.getForeground(),
										m_textField.getText()));
				dispose();

			}
		});
		m_fontField.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent arg0) {
				Font font = new Font((String) m_fontField.getSelectedItem(),
						getStyle(), (Integer) m_sizeSpinner.getValue());
				m_font = font;
				m_preview.setFont(font);
			}
		});
		m_textField.getDocument().addDocumentListener(new DocumentListener() {

			@Override
			public void removeUpdate(DocumentEvent arg0) {
				m_preview.setText(m_textField.getText());
			}

			@Override
			public void insertUpdate(DocumentEvent arg0) {
				m_preview.setText(m_textField.getText());
			}

			@Override
			public void changedUpdate(DocumentEvent arg0) {
				m_preview.setText(m_textField.getText());
			}
		});
		m_sizeSpinner.addChangeListener(new ChangeListenerFont());
		m_italicCheck.addChangeListener(new ChangeListenerFont());
		m_boldCheck.addChangeListener(new ChangeListenerFont());

		this.getContentPane().add(m_textLabel);
		this.getContentPane().add(m_textField);
		this.getContentPane().add(m_colorLabel);
		this.getContentPane().add(m_colorField);
		this.getContentPane().add(m_fontLabel);
		this.getContentPane().add(m_fontField);
		this.getContentPane().add(m_sizeLabel);
		this.getContentPane().add(m_sizeSpinner);
		this.getContentPane().add(m_italicCheck);
		this.getContentPane().add(m_boldCheck);
		this.getContentPane().add(m_confirmButton);
		this.getContentPane().add(m_cancelButton);
		this.getContentPane().add(m_preview);

	}

	public String[] getFontName() {
		return GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAvailableFontFamilyNames();
	}

	public int getStyle() {
		int bold_italic;
		if (m_italicCheck.isSelected()) {
			if (m_boldCheck.isSelected()) {
				bold_italic = Font.BOLD | Font.ITALIC;
			} else {
				bold_italic = Font.ITALIC;
			}
		} else {
			if (m_boldCheck.isSelected()) {
				bold_italic = Font.BOLD;
			} else {
				bold_italic = Font.PLAIN;
			}
		}
		return bold_italic;
	}

	public class ChangeListenerFont implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent arg0) {
			Font font = new Font((String) m_fontField.getSelectedItem(),
					getStyle(), (Integer) m_sizeSpinner.getValue());
			m_font = font;
			m_preview.setFont(font);
		}
	}
}
